{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uz-GROlhtK1x"
      },
      "source": [
        "Copyright 2023 The Google Research Authors.\n",
        "\n",
        "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "you may not use this file except in compliance with the License.\n",
        "You may obtain a copy of the License at\n",
        "\n",
        "http://www.apache.org/licenses/LICENSE-2.0\n",
        "\n",
        "Unless required by applicable law or agreed to in writing, software\n",
        "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "See the License for the specific language governing permissions and\n",
        "limitations under the License."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hu1PRUyW6gjt"
      },
      "outputs": [],
      "source": [
        "!git clone https://github.com/google-research/google-research.git\n",
        "!pip install tensorflow"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "53qq9aYWyJdj"
      },
      "outputs": [],
      "source": [
        "import sys\n",
        "sys.path.append('./google-research')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_BUoSRj8uEkw"
      },
      "outputs": [],
      "source": [
        "from android_in_the_wild import visualization_utils\n",
        "import tensorflow as tf\n",
        "import random"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZXVUKF7lOppb"
      },
      "outputs": [],
      "source": [
        "dataset_name = 'google_apps'  #@param [\"general\", \"google_apps\", \"install\", \"single\", \"web_shopping\"]\n",
        "\n",
        "dataset_directories = {\n",
        "    'general': 'gs://gresearch/android-in-the-wild/general/*',\n",
        "    'google_apps': 'gs://gresearch/android-in-the-wild/google_apps/*',\n",
        "    'install': 'gs://gresearch/android-in-the-wild/install/*',\n",
        "    'single': 'gs://gresearch/android-in-the-wild/single/*',\n",
        "    'web_shopping': 'gs://gresearch/android-in-the-wild/web_shopping/*',\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vd0lfiif-KNh"
      },
      "outputs": [],
      "source": [
        "filenames = tf.io.gfile.glob(dataset_directories[dataset_name])\n",
        "raw_dataset = tf.data.TFRecordDataset(filenames, compression_type='GZIP').as_numpy_iterator()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-XKAz8gJUBWS"
      },
      "outputs": [],
      "source": [
        "def get_episode(dataset):\n",
        "  \"\"\"Grabs the first complete episode.\"\"\"\n",
        "  episode = []\n",
        "  episode_id = None\n",
        "  for d in dataset:\n",
        "    ex = tf.train.Example()\n",
        "    ex.ParseFromString(d)\n",
        "    ep_id = ex.features.feature['episode_id'].bytes_list.value[0].decode('utf-8')\n",
        "    if episode_id is None:\n",
        "      episode_id = ep_id\n",
        "      episode.append(ex)\n",
        "    elif ep_id == episode_id:\n",
        "      episode.append(ex)\n",
        "    else:\n",
        "      break\n",
        "  return episode\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "KE4nM3IR0ezJ"
      },
      "outputs": [],
      "source": [
        "ep = get_episode(raw_dataset)\n",
        "visualization_utils.plot_episode(ep, show_annotations=True, show_actions=True)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "AndroidInTheWild Visualization Demo",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
